package com.quanxiaoha.xiaohashu.gateway.auth;


import cn.dev33.satoken.stp.StpInterface;
import cn.hutool.core.collection.CollUtil;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.google.common.collect.Lists;
import com.quanxiaoha.framework.redis.constant.RedisKeyConstants;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

/**
 * @Author: jingyan_liu
 * @CreateTime: 2025-07-12
 * @Description:
 * @Version: 1.0
 */


@Slf4j
@Component
public class StpInterfaceImpl implements StpInterface {
	
	@Resource
	private RedisTemplate<String, String> redisTemplate;
	@Resource
	private ObjectMapper objectMapper;
	
	@Override
	public List<String> getPermissionList(Object loginId, String loginType) {
		log.info("## 获取用户权限列表, loginId: {}", loginId);
		
		// 构建 用户-角色 Redis Key
		String userRolesKey = RedisKeyConstants.buildUserRoleKey(Long.valueOf(loginId.toString()));
		
		// 根据用户 ID ，从 Redis 中获取该用户的角色集合
		String useRolesValue = redisTemplate.opsForValue().get(userRolesKey);
		
		if (StringUtils.isBlank(useRolesValue)) {
			return null;
		}
		
		// 将 JSON 字符串转换为 List<String> 角色集合
		ArrayList<String> roleKeys;
		try {
			roleKeys = objectMapper.readValue(useRolesValue, new TypeReference<>() {
			});
		} catch (JsonProcessingException e) {
			log.error("解析权限列表失败, loginId: {}, error: {}", loginId, e.getMessage());
			return Collections.emptyList();
		}
		
		if (CollUtil.isNotEmpty(roleKeys)) {
			// 查询这些角色对应的权限
			// 构建 角色-权限 Redis Key 集合
			List<String> rolePermissionsKeys = roleKeys.stream().map(RedisKeyConstants::buildRolePermissionsKey).toList();
			
			// 通过 key 集合批量查询权限，提升查询性能。
			List<String> rolePermissionsValues = redisTemplate.opsForValue().multiGet(rolePermissionsKeys);
			
			if (CollUtil.isNotEmpty(rolePermissionsValues)) {
				List<String> permissions = Lists.newArrayListWithCapacity(5);
				
				// 遍历所有角色的权限集合，统一添加到 permissions 集合中
				rolePermissionsValues.forEach(jsonValue -> {
					try {
						// 将 JSON 字符串转换为 List<String> 权限集合
						ArrayList<String> rolePermissions = objectMapper.readValue(jsonValue, new TypeReference<>() {
						});
						permissions.addAll(rolePermissions);
					} catch (JsonProcessingException e) {
						log.error("==> JSON 解析错误: ", e);
					}
				});
				
				// 返回此用户所拥有的权限
				return permissions;
			}
		}
		return Collections.emptyList();
	}
	
	@Override
	public List<String> getRoleList(Object loginId, String loginType) {
		log.info("获取用户角色列表, loginId: {}", loginId);
		
		// 构建 用户-角色 Redis Key
		String userRolesKey = RedisKeyConstants.buildUserRoleKey(Long.valueOf(loginId.toString()));
		
		// 根据用户 ID ，从 Redis 中获取该用户的角色集合
		String useRolesValue = redisTemplate.opsForValue().get(userRolesKey);
		log.info("从 Redis 获取的值: {}", useRolesValue);
		if (StringUtils.isBlank(useRolesValue)) {
			return Collections.emptyList();
		}
		
		// 将 JSON 字符串转换为 List<String> 集合
		try {
			return objectMapper.readValue(useRolesValue, new TypeReference<>() {
			});
		} catch (Exception e) {
			log.error("解析用户角色列表失败, loginId: {}, error: {}", loginId, e.getMessage());
			return Collections.emptyList();
		}
	}
}
